#include "titlewidget.h"
#include <QVBoxLayout>
#include <QHBoxLayout>


TitleWidget::TitleWidget(QWidget *parent) : QWidget(parent)
{
    initUI();
    initConnection();
}

void TitleWidget::initUI()
{
    //标题栏
    m_verTitle = new QLabel();
    m_verTitle->setStyleSheet("color:white;");

    m_skinButton = new PushButton();
    m_mainMenuButton = new PushButton();
    m_minButton = new PushButton();
    m_feedbackButton = new PushButton();
    m_closeButton = new PushButton();
    m_medalButton = new PushButton();

    m_medalButton->setPicName(QString(":/img/sysButton/medal"));
    m_skinButton->setPicName(QString(":/img/sysButton/skin"));
    m_minButton->setPicName(QString(":/img/sysButton/min"));
    m_closeButton->setPicName(QString(":/img/sysButton/close"));
    m_mainMenuButton->setPicName(QString(":/img/sysButton/menu"));
    m_feedbackButton->setPicName(QString(":/img/sysButton/feedback"));

    QWidget *topWid = new QWidget();
    QHBoxLayout *title_layout = new QHBoxLayout(topWid);
    title_layout->addWidget(m_verTitle,0,Qt::AlignVCenter);
    title_layout->addStretch();
    title_layout->addWidget(m_medalButton, 0, Qt::AlignTop);
    title_layout->addWidget(m_skinButton, 0, Qt::AlignTop);
    title_layout->addWidget(m_feedbackButton, 0, Qt::AlignTop);
    title_layout->addWidget(m_mainMenuButton, 0, Qt::AlignTop);
    title_layout->addWidget(m_minButton, 0, Qt::AlignTop);
    title_layout->addWidget(m_closeButton, 0, Qt::AlignTop);
    title_layout->setSpacing(0);
    title_layout->setContentsMargins(0, 0, 5, 0);
    m_verTitle->setContentsMargins(15, 0, 0, 0);
    m_skinButton->setContentsMargins(0, 0, 10, 0);

    //按钮栏
    QStringList string_list;
    string_list<<":/img/toolWidget/tiJian"<<":/img/toolWidget/muMa"<<":/img/toolWidget/repair"<<":/img/toolWidget/qingLi"
        <<":/img/toolWidget/jiaSu"<<":/img/toolWidget/expert"<<":/img/toolWidget/menZhen"<<":/img/toolWidget/gongNeng";

    QHBoxLayout *button_layout = new QHBoxLayout();
    for(int i=0; i<string_list.size(); i++)
    {
        ToolButton *tool_button = new ToolButton(string_list.at(i));
        m_buttonList.append(tool_button);
        button_layout->addWidget(tool_button, 0, Qt::AlignBottom);
    }

    m_safe_360_label = new QLabel();
    m_safe_label = new QLabel();
    m_safe_360_label->setStyleSheet("color:white;");
    m_safe_label->setStyleSheet("color:white;");

    QVBoxLayout *safe_layout = new QVBoxLayout();
    safe_layout->addWidget(m_safe_360_label);
    safe_layout->addWidget(m_safe_label);
    safe_layout->setSpacing(5);
    safe_layout->setContentsMargins(0, 0, 0, 0);

    QLabel *logo_label = new QLabel();
    QPixmap pixmap(":/img/safe");
    logo_label->setPixmap(pixmap);
    logo_label->setFixedSize(pixmap.size());

    m_safe_360_label->setObjectName("whiteLabel");
    m_safe_label->setObjectName("whiteLabel");
    QFont safe_360_font = m_safe_360_label->font();
    safe_360_font.setPointSize(18);
    safe_360_font.setBold(true);
    m_safe_360_label->setFont(safe_360_font);

    m_safe_360_label->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
    m_safe_label->setAlignment(Qt::AlignHCenter | Qt::AlignTop);

    QFont safe_font = m_safe_label->font();
    safe_font.setPointSize(14);
    safe_font.setBold(true);
    m_safe_label->setFont(safe_font);

    button_layout->addStretch();
    button_layout->addLayout(safe_layout);
    button_layout->addWidget(logo_label);
    button_layout->setSpacing(8);
    button_layout->setContentsMargins(15, 0, 15, 0);

    //主框架
    QVBoxLayout *main_layout = new QVBoxLayout();
    main_layout->addWidget(topWid);
    main_layout->addLayout(button_layout);
    main_layout->setSpacing(0);
    main_layout->setContentsMargins(0, 0, 0, 0);
    setLayout(main_layout);

    translateLanguage();

    setFixedHeight(110);
}

void TitleWidget::initConnection()
{
    connect(m_skinButton, SIGNAL(clicked()), this, SIGNAL(showSkin()));
    connect(m_mainMenuButton, SIGNAL(clicked()), this, SIGNAL(showMainMenu()));
    connect(m_minButton, SIGNAL(clicked()), this, SIGNAL(showMin()));
    connect(m_closeButton, SIGNAL(clicked()), this, SIGNAL(closeWidget()));
}

void TitleWidget::translateLanguage()
{
    m_verTitle->setText(tr("360安全卫士") + QString("9.2"));
    m_skinButton->setToolTip(tr("change skin"));
    m_mainMenuButton->setToolTip(tr("main menu"));
    m_minButton->setToolTip(tr("minimize"));
    m_feedbackButton->setToolTip(tr("feedback"));
    m_closeButton->setToolTip(tr("close"));

    m_buttonList.at(0)->setText(tr("电源"));
    m_buttonList.at(1)->setText(tr("木马"));
    m_buttonList.at(2)->setText(tr("修复"));
    m_buttonList.at(3)->setText(tr("清理"));
    m_buttonList.at(4)->setText(tr("优化"));
    m_buttonList.at(5)->setText(tr("专家"));
    m_buttonList.at(6)->setText(tr("漏洞"));
    m_buttonList.at(7)->setText(tr("软件"));

    m_safe_360_label->setText(tr("360"));
    m_safe_label->setText(tr("safe"));
}

void TitleWidget::turnPage(QString current_page)
{
    bool ok;
    int current_index = current_page.toInt(&ok, 10);

    for(int i=0; i<m_buttonList.count(); i++)
    {
        ToolButton *tool_button = m_buttonList.at(i);
        if(current_index == i)
        {
            tool_button->setMousePress(true);
        }
        else
        {
            tool_button->setMousePress(false);
        }
    }


    emit turnPage(current_index);
}
